package com.hustzp.com.xichuangzhu.me;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.widget.NoScrollListView;

import java.util.List;

/**
 * Created by Administrator on 2016/6/5.
 */
public class MyBaseActivity extends FragmentActivity {

    protected PopupWindow sharePopupWindow;
    private View cancelView;
    private TextView popTitle;
    private NoScrollListView lvMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initSharePopupWindow();
    }

    public void initSharePopupWindow() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.popupwindow_layout, null);
        popTitle = (TextView) view.findViewById(R.id.pop_list_title);
        lvMenu = (NoScrollListView) view.findViewById(R.id.lvMenu);
        sharePopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        sharePopupWindow.setContentView(view);
        sharePopupWindow.setAnimationStyle(R.style.share_popwindow_anim_style);
        sharePopupWindow.setTouchable(true);
        sharePopupWindow.setFocusable(true);
        sharePopupWindow.setOutsideTouchable(true);
        sharePopupWindow.setBackgroundDrawable(new BitmapDrawable());

        cancelView = view.findViewById(R.id.cancel_id);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharePopupWindow.dismiss();
            }
        });
        cancelView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                sharePopupWindow.dismiss();
            }
        });
    }

    public void showSharePopupWindow(View view, String title, List<String> menus, AdapterView.OnItemClickListener listener) {
        if (title != null && !title.isEmpty()) {
            popTitle.setText(title);
        }

        bindData(menus, listener);
        sharePopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    public void bindData(List<String> menus, AdapterView.OnItemClickListener listener) {
        BaseAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.popupwindow_layout_item, R.id.tv_menu, menus);
        lvMenu.setAdapter(adapter);
        lvMenu.setOnItemClickListener(listener);
    }


    public void closePopupWindow() {
        sharePopupWindow.dismiss();
    }

    public void closeView(View view) {
        finish();
    }

    public void showToastInfo(String info) {
        Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
    }
}
